<template>
    <view class="my-movie">
        <!-- 自定义顶部栏 -->
        <view class="custom-header" :style="{paddingTop: statusBarHeight + 'px'}">
            <view class="left-area" @tap="goBack">
                <image src="/static/images/back.png" mode="aspectFit" class="icon-back"></image>
            </view>
            <view class="center-area">
                <text class="name">想看的电影</text>
            </view>
            <view class="right-area"></view>
        </view>
        
        <!-- 电影列表区域 -->
        <view class="movie-container">
            <!-- 有电影时显示列表 -->
            <view class="movie-list" v-if="wishMovie.length">
                <view class="movie-item" v-for="(item, index) in wishMovie" :key="index">
                    <!-- 左侧海报 -->
                    <view class="poster-wrapper" @tap="goToMovieDetail(item.movie_id)">
                        <image :src="getPoster(item.poster)" mode="aspectFill" class="poster-image"></image>
                    </view>
                    
                    <!-- 中间信息区 -->
                    <view class="movie-info">
                        <!-- 电影名称 -->
                        <view class="info-row">
                            <text class="movie-name" @tap="goToMovieDetail(item.movie_id)">{{item.movie_name}}</text>
                        </view>
                        <!-- 想看人数 -->
                        <view class="info-row">
                            <text class="movie-wish">想看：<text class="number">{{item.wish_num || 0}}</text> 人想看</text>
                        </view>
                        <!-- 主演信息 -->
                        <view class="info-row">
                            <text class="movie-actor">主演：{{item.actor || '暂无信息'}}</text>
                        </view>
                    </view>
                    
                    <!-- 右侧购票按钮 -->
                    <view class="btn-area">
                        <button class="buy-btn" @tap="goToSelectCinema(item.movie_id)">购票</button>
                    </view>
                </view>
            </view>
            
            <!-- 无电影时显示空状态 -->
            <view class="empty-container" v-else>
                <image src="/static/images/empty.png" mode="aspectFit" class="empty-image"></image>
                <text class="empty-text">暂无想看的影片</text>
            </view>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            wishMovie: [],
            statusBarHeight: 20 // 默认状态栏高度
        }
    },
    onLoad(options) {
        // 获取系统状态栏高度
        const systemInfo = uni.getSystemInfoSync();
        this.statusBarHeight = systemInfo.statusBarHeight;
        
        if (options.UID) {
            this.loadWishMovieInfo(options.UID);
        }
    },
    methods: {
        goBack() {
            uni.navigateBack();
        },
        loadWishMovieInfo(userId) {
            uni.showLoading({
                title: '加载中...'
            });
            
            uni.request({
                url: 'http://localhost:9090/getUserLikeMovie',
                method: 'GET',
                data: {
                    userId: userId
                },
                success: (res) => {
                    uni.hideLoading();
                    if (res.data.code === 1) {
                        this.wishMovie = res.data.movies;
                        // 按上映日期排序
                        this.wishMovie.sort((a, b) => {
                            return new Date(a.public_date) - new Date(b.public_date);
                        });
                    } else {
                        uni.showToast({
                            title: '获取想看电影失败',
                            icon: 'none'
                        });
                    }
                },
                fail: (err) => {
                    uni.hideLoading();
                    console.log(err);
                    uni.showToast({
                        title: '网络请求失败',
                        icon: 'none'
                    });
                }
            });
        },
        getPoster(path) {
            return 'http://localhost:9090' + path;
        },
        goToMovieDetail(movieId) {
            uni.navigateTo({
                url: `/pages/movie-detail/movie-detail?movie_id=${movieId}`
            });
        },
        goToSelectCinema(movieId) {
            uni.navigateTo({
                url: `/pages/select-cinema/select-cinema?movie_id=${movieId}`
            });
        }
    }
}
</script>

<style>
.my-movie {
    width: 100%;
    min-height: 100vh;
    color: #000;
    background-color: #f1f1f1;
    display: flex;
    flex-direction: column;
}

/* 自定义顶部栏样式 */
.custom-header {
    width: 100%;
    height: 90rpx;
    background-color: #dd2727;
    display: flex;
    align-items: center;
    position: relative;
    z-index: 100;
}

.left-area {
    width: 100rpx;
    height: 90rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    left: 0;
}

.icon-back {
    width: 40rpx;
    height: 40rpx;
}

.center-area {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
}

.name {
    color: #ffffff;
    font-size: 36rpx;
    font-weight: 500;
    text-align: center;
}

.right-area {
    width: 100rpx;
    height: 90rpx;
}

/* 电影列表样式 */
.movie-container {
    flex: 1;
    padding: 20rpx;
    box-sizing: border-box;
    padding-bottom: env(safe-area-inset-bottom); /* 适配全面屏底部安全区域 */
}

.movie-list {
    display: flex;
    flex-direction: column;
}

.movie-item {
    display: flex;
    align-items: center;
    padding: 20rpx;
    margin-bottom: 20rpx;
    background-color: #fff;
    border-radius: 12rpx;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
    height: 100%;
}

.poster-wrapper {
    width: 120rpx;
    height: 160rpx;
    margin-right: 20rpx;
    border-radius: 8rpx;
    overflow: hidden;
    flex-shrink: 0;
}

.poster-image {
    width: 100%;
    height: 100%;
}

.movie-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    max-width: calc(100% - 260rpx); /* 减去海报宽度和按钮宽度 */
}

.info-row {
    margin-bottom: 8rpx;
}

.movie-name {
    font-size: 30rpx;
    font-weight: bold;
    color: #333;
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.movie-wish {
    font-size: 25rpx;
    color: #9d9d9d;
}

.movie-actor {
    font-size: 25rpx;
    color: #9d9d9d;
    width: 100%;
    white-space: normal;
}

.number {
    color: #ffb400;
    font-weight: bold;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ellipsis {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.btn-area {
    width: 120rpx;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    flex-shrink: 0;
}

.buy-btn {
    width: 100rpx;
    height: 60rpx;
    line-height: 60rpx;
    text-align: center;
    background-color: #dd2727;
    color: #fff;
    font-size: 24rpx;
    border-radius: 30rpx;
    padding: 0;
}

/* 空状态样式 */
.empty-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding-top: 100rpx;
}

.empty-image {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 20rpx;
}

.empty-text {
    color: #999;
    font-size: 28rpx;
}
</style>